package com.example.ireader;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.internal.BottomNavigationItemView;
import android.support.design.internal.BottomNavigationMenuView;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.lang.reflect.Field;

public class MainActivity extends AppCompatActivity {

    private TextView mTextMessage;
    ViewPager viewPager;
    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_read:
                    //mTextMessage.setText(R.string.title_read);
                    viewPager.setCurrentItem(0);
                    return true;
                case R.id.navigation_bookcity:
                    //mTextMessage.setText(R.string.title_bookcity);
                    viewPager.setCurrentItem(1);
                    return true;
                case R.id.navigation_discovery:
                    //mTextMessage.setText(R.string.title_discovery);
                    viewPager.setCurrentItem(2);
                    return true;
                case R.id.navigation_mine:
                    //mTextMessage.setText(R.string.title_mine);
                    viewPager.setCurrentItem(3);
                    return true;
            }
            return false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTextMessage = (TextView) findViewById(R.id.message);
        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        navigation.setItemIconTintList(null);
        disableShiftMode(navigation);

        viewPager=findViewById(R.id.view_pager);
        ViewPagerAdapter viewPagerAdapter=new ViewPagerAdapter(getSupportFragmentManager());

        //获取页面

        viewPagerAdapter.addFragment(ReadFragment.getInstance());
        viewPagerAdapter.addFragment(BookcityFragment.getInstance());
        viewPagerAdapter.addFragment(DiscoveryFragment.getInstance());
        viewPagerAdapter.addFragment(MineFragment.getInstance());

        viewPager.setAdapter(viewPagerAdapter);

//        // ---------------------------------Category_First_Page部分-------------------------------//
//        Button Button_Category = (Button)findViewById(R.id.Button_Category);
//        Button_Category.setOnClickListener(new View.OnClickListener(){
//            @Override
//            public void onClick(View v){
//                //显式方法打开SecondActivity
//                Intent intent_category = new Intent(MainActivity.this,CategoryActivity.class);
//                startActivity(intent_category);
//            }
//        });
//        // ---------------------------------Category_First_Page部分-------------------------------//
    }
    /*禁用ButtonNavigationView动画效果*/
    @SuppressLint("RestrictedApi")
    private void disableShiftMode(BottomNavigationView navigationView) {
        BottomNavigationMenuView menuView = (BottomNavigationMenuView) navigationView.getChildAt(0);
        try {
            Field shiftingMode = menuView.getClass().getDeclaredField("mShiftingMode");
            shiftingMode.setAccessible(true);
            shiftingMode.setBoolean(menuView, false);
            shiftingMode.setAccessible(false);
            for (int i = 0; i < menuView.getChildCount(); i++) {
                BottomNavigationItemView itemView = (BottomNavigationItemView) menuView.getChildAt(i);
                itemView.setShiftingMode(false);
                itemView.setChecked(itemView.getItemData().isChecked());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //再按一次退出
    private long exitTipTime = 0;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK){
            if (System.currentTimeMillis() - exitTipTime <= 1000){
                return super.onKeyDown(keyCode, event); // 由于是在入口 Activity 中，我们交给 super，它会帮我们退出。
            }
            else {
                exitTipTime = System.currentTimeMillis();
                Toast.makeText(this, "再按一次退出掌阅", Toast.LENGTH_SHORT).show();
                return true; // 返回 true，表示我已经处理了 onKeyDown，你们其他人不要再管了。
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}
